package com.csust.cg.mongodb.service.impl;

import com.csust.cg.mongodb.domain.Result;
import com.csust.cg.mongodb.domain.vo.DatabaseVO;
import com.csust.cg.mongodb.service.DatabasesService;
import com.csust.cg.mongodb.utils.MongoUtils;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoDatabase;
import org.bson.BsonDocument;
import org.bson.BsonInt64;
import org.bson.Document;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author chenguang
 */
@Service
public class DatabasesServiceImpl implements DatabasesService {
    @Override
    public Result getTreeList(String connectionId) {
        MongoClient client = MongoUtils.getClient(connectionId);
        List<Map<String, List<String>>> res = new ArrayList<>();
        List<String > databaseNames = new ArrayList<>();
        client.listDatabaseNames().forEach(databaseNames::add);
        databaseNames.forEach(item -> {
            Map<String, List<String>> databases = new TreeMap<>();
            MongoDatabase database = client.getDatabase(item);
            List<String> collections = new LinkedList<>();
            for (String collection : database.listCollectionNames()) {
                if ("system.sessions".equals(collection) || "system.version".equals(collection)) {
                    continue;
                }
                collections.add(collection);
            }
            databases.put(item, collections);
            res.add(databases);
        });
        MongoUtils.closeClient(client);
        return Result.success(res);
    }

    @Override
    public Result getDatabasesInfoList(String connectionId) {
        MongoClient client = MongoUtils.getClient(connectionId);
        List<DatabaseVO> list = new LinkedList<>();
        client.listDatabaseNames().forEach(database -> {
            MongoDatabase db = client.getDatabase(database);
            Document dbStats = db.runCommand(new BsonDocument("dbStats", new BsonInt64(1)));
            DatabaseVO vo = new DatabaseVO();
            vo.setName(dbStats.getString("db"));
            vo.setCollections(dbStats.getInteger("collections"));
            vo.setViews(dbStats.getInteger("views"));
            vo.setObjects(dbStats.getInteger("objects"));
            vo.setAvgObjSize(dbStats.getDouble("avgObjSize"));
            vo.setDataSize(dbStats.getDouble("dataSize"));
            vo.setStorageSize(dbStats.getDouble("storageSize"));
            vo.setIndexes(dbStats.getInteger("indexes"));
            vo.setIndexSize(dbStats.getDouble("indexSize"));
            vo.setTotalSize(dbStats.getDouble("totalSize"));
            list.add(vo);
        });
        MongoUtils.closeClient(client);
        return Result.success(list);
    }

    @Override
    public Result createDatabaseAndCollection(String connectionId, String databaseName, String collectionName) {
        MongoClient client = MongoUtils.getClient(connectionId);
        client.getDatabase(databaseName).createCollection(collectionName);
        MongoUtils.closeClient(client);
        return Result.success();
    }

    @Override
    public Result dropDatabase(String connectionId, String databaseName) {
        MongoClient client = MongoUtils.getClient(connectionId);
        client.getDatabase(databaseName).drop();
        MongoUtils.closeClient(client);
        return Result.success();
    }
}
